package Client.PokerClient.src;

import javax.swing.JFrame;

/**
 * Klasa glowna programu gry w pokera.
 * @author Xsar
 */
public class Client {
  /**
   * Referencja do obiektu kontrolujacego rozgrywke.
   */
  private GameController gameController;
  /**
   * Referencja do obiektu kontrolujacego lobby.
   */
  private ServerController serverController;

  /**
   * Glowne okno programu.
   */
  private JFrame window;
  /**
   * Klasa tworzaca okno menu programu.
   */
  private MenuWindow menuWindow;
  /**
   * Klasa tworzaca okno do polaczania sie z serwerem.
   */
  private ServerConnectionWindow serverConnectionWindow;
  /**
   * Klasa tworzaca okno lobby programu.
   */
  private ServerWindow serverWindow;
  /**
   * Klasa tworzaca okno gry programu.
   */
  private GameWindow gameWindow;
  /**
   * Watek zarzadzajacy polaczeniem z serverem.
   */
  private GameThread gameThread;
  /**
   * Okno tworzenia nowej gry.
   */
  private CreateGameWindow createGameWindow;
  /**
   * 
   */
  GameMouseListener gameMouseListener;
  /**
   * Konstruktor domyslny programu.
   */
  public Client() {
	window = new JFrame();
    window.setResizable(false);
    window.addWindowListener(new WinAdap(this));
    gameThread = new GameThread(this);
    menuWindow = new MenuWindow(window, 400, 200);
    menuWindow.addActionListener(new MenuListener(this, menuWindow));
    serverConnectionWindow = new ServerConnectionWindow(window, 400, 200);
    serverConnectionWindow.addActionListener(new ServerConnectionListener(this, serverConnectionWindow));
    menuWindow.ChangeWindow();
    window.setVisible(true);
  }

  /**
   * Konstruktor testow klasy.
   * @param Test Jesli true tworzona jest minimalna ilosc danych w grze. Jesli false nie jest wyswietlane okno programu,
   * ani nie sa dodawan listenery.
   */
  public Client(boolean Test) {
	if(Test) {
	  window = new JFrame();
      gameThread = new GameThread(this);
	}
	else {
	  window = new JFrame();
      gameThread = new GameThread(this);
	  window.addWindowListener( new WinAdap(this) );
      gameThread = new GameThread(this);
      menuWindow = new MenuWindow(window, 400, 200);
      menuWindow.addActionListener(new MenuListener(this, menuWindow));
      serverConnectionWindow = new ServerConnectionWindow(window, 400, 200);
      serverConnectionWindow.addActionListener(new ServerConnectionListener(this, serverConnectionWindow));
	}
  }
  /**
   * Zwraca ServerController.
   * @return ServerController.
   */
  public ServerController getServerController() {
    return serverController;
  }
  /**
   * Zwraca GameController.
   * @return GameController
   */
  public GameController getGameController() {
    return gameController;
  }
  /**
   * Zwraca JFrame.
   * @return JFrame
   */
  public JFrame getMainWindow() {
    return window;
  }
  /**
   * Zwraca MenuWindow.
   * @return MenuWindow
   */
  public MenuWindow getMenuWindow() {
    return menuWindow;
  }
  /**
   * Zwraca ServerConnectionWindow.
   * @return ServerConnectionWindow
   */
  public ServerConnectionWindow getServerConectionWindow() {
    return serverConnectionWindow;
  }
  /**
   * Zwraca ServerWindow.
   * @return ServerWindow.
   */
  public ServerWindow getServerWindow() {
    return serverWindow;
  }
  /**
   * Zwraca GameWindow.
   * @return GameWindow.
   */
  public GameWindow getGameWindow() {
	return gameWindow;
  }
  /**
   * Zwraca GameThread.
   * @return GameThread.
   */
  public GameThread getGameThread() {
	return gameThread;
  }
  /**
   * Zwraca CreateGameWindow.
   * @return CreateGameWindow
   */
  public CreateGameWindow getCreateGameWindow() {
    return createGameWindow;
  }
  /**
   * Metoda tworzaca okno to utworzenia nowej gry.
   */
  public void CreateGameWindow() {
    createGameWindow = new CreateGameWindow(window);
    createGameWindow.toFront();
    createGameWindow.addActionListener( new CreateGameListener(this, createGameWindow) );
    createGameWindow.setVisible(true);
  }
  /**
   * Metoda tworzaca nowa gre.
   * @param actualNumberOfPlayers Aktualna liczba graczy w grze.
   * @param numberOfBot Liczba botow w grze.
   * @param maxPlayers Maksymalna liczba graczy.
   * @param isPlayerHost Czy gracz jest hostem.
   * @param isHostPlay Czy gracz gra.
   */
  public void CreateGame(int actualNumberOfPlayers, int numberOfBot, int maxPlayers, boolean isPlayerHost, boolean isHostPlay) {
    window.removeMouseListener(gameMouseListener);
    gameMouseListener = new GameMouseListener(this); 
    if(isHostPlay) { window.addMouseListener(gameMouseListener); }
	gameController = new GameController(actualNumberOfPlayers, numberOfBot, maxPlayers, isPlayerHost, isHostPlay, this);
    gameWindow = new GameWindow(window, 800, 600);
    gameWindow.addActionListener(new GameListener(this, gameWindow));
  }
  /**
   * Metoda tworzaca okno servera (lobby).
   */
  public void CreateServerWindow() {
	serverController = new ServerController(this);
    serverWindow = new ServerWindow(window, 500, 300, serverController.getServerDatabase());
    serverWindow.addActionListener(new ServerListener(this, serverWindow));
  }
}
